package com.huo.consumer;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ConsumerListener {
    private static String activeMqUrl = "tcp://localhost:61616";
    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(activeMqUrl);
        Connection connection = null;
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
            //创建目的地
            Destination destination = session.createQueue("myQueue");
            //创建发送者
            MessageConsumer  messageConsumer = session.createConsumer(destination);

            MessageListener messageListener =  new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    try {
                        System.out.println(((TextMessage)message).getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            };
            while (true){
                messageConsumer.setMessageListener(messageListener);
                session.commit();
            }

        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if(connection != null){
                connection.close();
            }
        }
    }

}
